#include "Config.h"

namespace great_config {
	Config::Config(const string &file) {
		_file = file;
		loadConfig();
	}

	const string Config::get(const string &key) {
		return _config[key];
	}

	Config::~Config() {
		_config.clear();
	}

	bool Config::loadConfig() {
		ifstream in(_file.c_str(), ifstream::in);

		char buffer[1024];
		char *pos = NULL;

		_config.clear();
		while(in.good()) {
			in.getline(buffer, sizeof(buffer));
			if(buffer[0] == '#' || buffer[0] == ' ' || strlen(buffer) == 0) {
				continue;
			}
			pos = strchr(buffer, '=');
			buffer[pos - buffer] = '\0';
			_config[Util::trim(buffer)] = Util::trim(pos + 1);
		}
		in.close();
		return true;
	}
}
